/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testmenu;

import Utilities.Preference;
import Utilities.TrainingUtilities;
import View.AboutForm;
import View.TrainedForm;
import View.TrainingForm;
import View.heathtipForm;
import View.mainMenuForm;
import View.settingForm;
import com.sun.lwuit.Display;
import javax.microedition.midlet.*;
import javax.microedition.rms.RecordStoreException;

/**
 * @author Windsea
 */
public class MainMidlet extends MIDlet {

    private String var_Yourname;
    private int var_Age;
    private boolean var_Gender;
    private double var_Height;
    private double var_Weight;
    private double var_BMR;
    private double var_BMI;
    private int var_WeightMode;
    private boolean var_FirstTime = false;
    double[] activity_mode = new double[4];
    private TrainingForm form_Trainning;
    private heathtipForm form_healthtipl;
    private settingForm form_setting;
    private mainMenuForm mmf;
    private AboutForm abf;
    private TrainedForm form_trainedForm;

    // khoi tao form va data
    private void Initializing() {
        activity_mode[0] = 2.3;//walking, 2.5 mph (4 km/h)
        activity_mode[1] = 2.9;//walking, 2.5 mph (4.8 km/h)
        activity_mode[2] = 4.0;//bicycling, <10 mph (16 km/h), leisure, to work or for pleasure
        this.form_setting = new settingForm(this);
        this.form_healthtipl = new heathtipForm(this);
        this.mmf = new mainMenuForm(this);
        this.form_trainedForm = new TrainedForm(this);
        ShowMainMenuForm();
    }

    public void startApp() {
        Display.init(this);
        Display.getInstance().setForceFullScreen(true);
        Initializing();
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
        Display.getInstance().exitApplication();
    }

    public void saveUserData() {
        try {
            Preference p = new Preference("UserData");
            p.put("Yourname", this.var_Yourname);
            p.put("Age", this.var_Age + "");
            p.put("Gender", this.var_Gender + "");
            p.put("Height", this.var_Height + "");
            //p.put("Height",172+ "");
            p.put("Weight", this.var_Weight + "");
            p.put("WeightMode", this.var_WeightMode + "");
            p.save();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
    }

    public boolean readUserData() {
        try {
            Preference p = new Preference("UserData");
            this.var_Yourname = p.get("Yourname");
            this.var_Age = Integer.parseInt(p.get("Age"));
            String Gender = p.get("Gender");
            if (Gender.equals("true")) {
                this.var_Gender = true;
            } else {
                this.var_Gender = false;
            }
            this.var_Height = Double.parseDouble(p.get("Height"));
            this.var_Weight = Double.parseDouble(p.get("Weight"));
            this.var_WeightMode = Integer.parseInt(p.get("WeightMode"));
            System.out.println(var_Yourname + " " + var_Age + " " + var_Gender + " " + var_Height + " " + var_Weight + " " + var_WeightMode);
            TrainingUtilities u = new TrainingUtilities();
            
            var_BMR = u.calc_BMR(var_Gender, var_Weight, var_Height, var_Age);
            var_BMI = u.calc_BMI(var_Weight, var_Height);
            this.form_Trainning = new TrainingForm(this);
            return true;
        } catch (Exception ex) {
            this.var_FirstTime = true;
            System.out.println("First time");
            return false;
        }
    }

    public String getVar_Yourname() {
        return var_Yourname;
    }

    public void setVar_Yourname(String var_Yourname) {
        this.var_Yourname = var_Yourname;
    }

    public int getVar_Age() {
        return var_Age;
    }

    public void setVar_Age(int var_Age) {
        this.var_Age = var_Age;
    }

    public boolean isVar_Gender() {
        return var_Gender;
    }

    public void setVar_Gender(boolean var_Gender) {
        this.var_Gender = var_Gender;
    }

    public double getVar_Height() {
        return var_Height;
    }

    public void setVar_Height(double var_Height) {
        this.var_Height = var_Height;
    }

    public double getVar_Weight() {
        return var_Weight;
    }

    public void setVar_Weight(double var_Weight) {
        this.var_Weight = var_Weight;
    }

    public double getVar_BMR() {
        return var_BMR;
    }

    public void setVar_BMR(double var_BMR) {
        this.var_BMR = var_BMR;
    }

    public double getVar_BMI() {
        return var_BMI;
    }

    public void setVar_BMI(double var_BMI) {
        this.var_BMI = var_BMI;
    }

    public int getVar_WeightMode() {
        return var_WeightMode;
    }

    public void setVar_WeightMode(int var_WeightMode) {
        this.var_WeightMode = var_WeightMode;
    }

    public boolean isVar_FirstTime() {
        return var_FirstTime;
    }

    public void setVar_FirstTime(boolean var_FirstTime) {
        this.var_FirstTime = var_FirstTime;
    }

    public double[] getActivity_mode() {
        return activity_mode;
    }

    public void setActivity_mode(double[] activity_mode) {
        this.activity_mode = activity_mode;
    }

    public TrainingForm getForm_Trainning() {
        return form_Trainning;
    }

    public void setForm_Trainning(TrainingForm form_Trainning) {
        this.form_Trainning = form_Trainning;
    }

    public void ShowTrainingForm() {
        this.form_Trainning.show();
    }

    public void showHealthtipForm() {
        this.form_healthtipl.show();
    }

    public settingForm getForm_setting() {
        return form_setting;
    }

    public void setForm_setting(settingForm form_setting) {
        this.form_setting = form_setting;
    }

    public void showSettingForm() {
        this.form_setting.show();
    }

    public void ShowMainMenuForm() {
        this.mmf.show();
    }

    public mainMenuForm getmmf() {
        return this.mmf;
    }
    
    public void initTrainingForm(){
        this.form_Trainning = new TrainingForm(this);
    }

    public TrainedForm getForm_trainedForm() {
        return form_trainedForm;
    }

    public void setForm_trainedForm(TrainedForm form_trainedForm) {
        this.form_trainedForm = form_trainedForm;
    }
    
    public void showTrainedForm(){
        this.form_trainedForm.show();
    }
}
